﻿using System;
using System.Windows.Controls;
using System.Windows.Interactivity;

namespace Tequila.Phone.Interactivity
{
    /// <summary>
    /// Open SIP panel automatically when text box is loaded
    /// </summary>
    public class TextBoxAutoFocusBehavior: Behavior<TextBox>
    {
        /// <summary>
        /// Called after the behavior is attached to an AssociatedObject.
        /// </summary>
        /// <remarks>
        /// Override this to hook up functionality to the AssociatedObject.
        /// </remarks>
        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.Loaded += OnTextBoxLoaded;
        }

        /// <summary>
        /// Called when the behavior is being detached from its AssociatedObject, but before it has actually occurred.
        /// </summary>
        /// <remarks>
        /// Override this to unhook functionality from the AssociatedObject.
        /// </remarks>
        protected override void OnDetaching()
        {
            base.OnDetaching();
            AssociatedObject.Loaded -= OnTextBoxLoaded;
        }

        private void OnTextBoxLoaded(object sender, EventArgs args)
        {
            AssociatedObject.Focus();
        }
    }
}
